package com.example.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


//@Component
@Slf4j
@Aspect
public class AspectDemo {
    @Pointcut("execution(* com.example.aop.controller.*.*(..))")
    public void pt(){};

    //前置通知
    @Before("execution(* com.example.aop.controller.*.*(..))")
    public void doBefore() {
        log.info("AspectDemo do before...");
    }
    //后置通知
    @After("execution(* com.example.aop.controller.*.*(..))")
    public void doAfter() {
        log.info("AspectDemo do after...");
    }
    //环绕通知
    @Around("execution(* com.example.aop.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("AspectDemo do around before...");
        Object result = null;
        try {
            result = joinPoint.proceed();
        }catch (Exception e) {
            log.error("do around 执行目标函数, 内部发生异常");
        }
        log.info("AspectDemo do around after...");
        return result;
    }
    //返回后通知
    @AfterReturning("execution(* com.example.aop.controller.*.*(..))")
    public void doAfterReturning() {
        log.info("AspectDemo do AfterReturning...");
    }
    //抛出异常后通知
    @AfterThrowing("execution(* com.example.aop.controller.*.*(..))")
    public void doAfterThrowing() {
        log.info("AspectDemo do AfterThrowing...");
    }
}
//@Component
//@Slf4j
//@Aspect
//public class AspectDemo {
//    @Pointcut("execution(* com.example.aop.controller.*.*(..))")
//    public void pt(){};
//
//    //前置通知
//    @Before("pt()")
//    public void doBefore() {
//        //...代码省略
//    }
//    //后置通知
//    @After("pt()")
//    public void doAfter() {
//        //...代码省略
//    }
//    //添加环绕通知
//    @Around("pt()")
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        //...代码省略
//    }
//    //返回后通知
//    @AfterReturning("pt()")
//    public void doAfterReturning() {
//        //...代码省略
//    }
//    //抛出异常后通知
//    @AfterThrowing("pt()")
//    public void doAfterThrowing() {
//        //...代码省略
//    }
//}


